package cn.woblog.recyclerviewsample.adapter;

import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import cn.woblog.recyclerviewsample.BaseRecyclerViewAdapter;
import cn.woblog.recyclerviewsample.R;
import cn.woblog.recyclerviewsample.SwipeRecyclerViewActivity;

/**
 * Created by Pich on 2017/4/4.
 */

public class SwipeRecyclerViewAdapter extends
    BaseRecyclerViewAdapter<String, SwipeRecyclerViewAdapter.ViewHolder> {

  public SwipeRecyclerViewAdapter(Context context) {
    super(context);
  }

  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new ViewHolder(getInflater().inflate(R.layout.item_swipe_item, parent, false));
  }

  @Override
  public void onBindViewHolder(final ViewHolder holder, int position) {
    super.onBindViewHolder(holder, position);
    holder.bindData(getData(position));
    holder.iv_icon.setOnTouchListener(new OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
        ((SwipeRecyclerViewActivity) getContext()).startDrag(holder);
        return true;
      }
    });

    holder.iv_delete.setOnTouchListener(new OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
        ((SwipeRecyclerViewActivity) getContext()).startSwipe(holder);
        return true;
      }
    });
  }

  public class ViewHolder extends BaseRecyclerViewAdapter.ViewHolder {

    final ImageView iv_icon;
    final ImageView iv_delete;
    private final TextView tv;

    public ViewHolder(View itemView) {
      super(itemView);
      tv = (TextView) itemView.findViewById(android.R.id.text1);
      iv_icon = (ImageView) itemView.findViewById(R.id.iv_icon);
      iv_delete = (ImageView) itemView.findViewById(R.id.iv_delete);
    }

    public void bindData(String data) {
      tv.setText(data);
    }
  }
}
